package hn.cch.controller.v0;

import hn.cch.configuration.ApplicationConfiguration;
import hn.cch.entity.request.TestPostRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.mvc.method.annotation.StreamingResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.nio.charset.StandardCharsets;


@RequestMapping(value = "/v0/test")
@RestController
public class TestController {

    private static final Logger logger = LoggerFactory.getLogger(TestController.class);

    @Autowired
    private ApplicationConfiguration applicationConfiguration;

    @GetMapping(value = "/get")
    @ResponseBody
    public String get(@RequestParam String reportDate){
        logger.debug("report:{}", reportDate);
        return "get";
    }

    @PostMapping(value = "/post")
    @ResponseBody
    public String post(@RequestBody TestPostRequest testPostRequest){
        logger.debug("report:{}", testPostRequest.getReportDate());
        return "post";
    }

    @GetMapping(value = "/parameter")
    @ResponseBody
    public String parameter(){
        logger.info("get");
        String parameter = applicationConfiguration.getParameter();
        logger.info("parameter:{}", parameter);
        return parameter;
    }

    public void export(HttpServletResponse response){

    }
    @GetMapping(value = "/streaming")
    public StreamingResponseBody getStreaming(HttpServletResponse response) {
        return outputStream -> {
            outputStream.write("outputStream".getBytes(StandardCharsets.UTF_8));
            // try (ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream)) {
            //     objectOutputStream.writeBytes("ObjectOutputStream");
            // }
        };

    }

}
